﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace Skylabs.Breeze
{
    public partial class frmTransferFile : Form
    {
        private FileTransferer ThisTransfer;
        private TimeSpan TransferTime;
        private long StarTime = 0;
        public frmTransferFile()
        {
            InitializeComponent();
        }

        private void frmTransferFile_Load(object sender, EventArgs e)
        {
            NetworkingStuff.ListNetworkComputers(onListComplete);
        }
        private void onListComplete(List<DirectoryEntry> comps)
        {
            this.Invoke(new Action(() =>
                {
                    comboBox1.Items.Clear();
                    comboBox1.Enabled = true;
                    foreach (DirectoryEntry de in comps)
                    {
                        comboBox1.Items.Add(de);
                        IPHostEntry ihe = Dns.GetHostEntry(de.Name);
                    }                                           
                }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            switch (button2.Text)
            {
                case "Transfer":
                    {
                        DirectoryEntry dr = comboBox1.SelectedItem as DirectoryEntry;
                        string constring = comboBox1.Text;
                        //TODO Need to fix connect by workstation name. It causes some kind of null reference or some dumb junk.
                        if (dr != null)
                        {
                            IPHostEntry ihe = Dns.GetHostEntry(dr.Name);
                            IPAddress ip = ihe.AddressList.Where(i => i.AddressFamily == AddressFamily.InterNetwork).First();
                            if(ip != null)
                                constring = ip.ToString();
                        }
                        ThisTransfer = new FileTransferer(textBox1.Text, constring);
                        ThisTransfer.OnComplete += new EventHandler(ThisTransfer_OnComplete);
                        timer1.Enabled = true;
                        button2.Enabled = false;
                        TransferTime = new TimeSpan(1L);
                        StarTime = DateTime.Now.Ticks;
                        ThisTransfer.Start();
                        break;
                    }
                case "Close":
                    {
                        this.Close();
                        break;
                    }
            }
        }

        void ThisTransfer_OnComplete(object sender, EventArgs e)
        {
            Invoke(new Action(()=>
                {
                    timer1.Enabled = false;
                    button2.Text = "Close";
                    button2.Enabled = true;                  
                }));

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value = ThisTransfer.Progress;
            TransferTime = TransferTime.Add(TimeSpan.FromMilliseconds(100));
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - StarTime);
            long persec = (long)(ThisTransfer.TotalBytesSent/ts.TotalSeconds);
            label1.Text = ToFileSize(persec);

        }
        private static string ToFileSize(int source)
        {
            return ToFileSize(Convert.ToInt64(source));
        }

        private static string ToFileSize(long source)
        {
            const int byteConversion = 1024;
            double bytes = Convert.ToDouble(source);

            if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
            {
                return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB/s");
            }
            else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
            {
                return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB/s");
            }
            else if (bytes >= byteConversion) //KB Range
            {
                return string.Concat(Math.Round(bytes / byteConversion, 2), " KB/s");
            }
            else //Bytes
            {
                return string.Concat(bytes, " Bytes/s");
            }
        }
    }
}
